package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.TroubleType;
import cn.stylefeng.guns.modular.system.entity.Unit;
import cn.stylefeng.guns.modular.system.model.params.UnitParam;
import cn.stylefeng.guns.modular.system.model.result.resultVO.TroubleTypeVO;
import cn.stylefeng.guns.modular.system.model.result.resultVO.TypeVO;
import cn.stylefeng.guns.modular.system.service.ProductOrderService;
import cn.stylefeng.guns.modular.system.service.TroubleListService;
import cn.stylefeng.guns.modular.system.service.TroubleTypeService;
import cn.stylefeng.guns.modular.system.service.UnitService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 产品单位表控制器
 *
 * @author 小飞
 * @Date 2019-06-27 14:22:10
 */
@Controller
@RequestMapping("/unit")
public class UnitController extends BaseController {

    private String PREFIX = "/unit";

    @Autowired
    private UnitService unitService;
    @Autowired
    private TroubleTypeService troubleTypeService;
    @Autowired
    private TroubleListService troubleListService;
    @Autowired
    private ProductOrderService productOrderService;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/unit.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/unit_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/unit_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(UnitParam unitParam) {
        this.unitService.add(unitParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(UnitParam unitParam) {
        this.unitService.update(unitParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(UnitParam unitParam) {
        this.unitService.delete(unitParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(UnitParam unitParam) {
        Unit detail = this.unitService.getById(unitParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(UnitParam unitParam) {
        return this.unitService.findPageBySpec(unitParam);
    }


    @RequestMapping("/unitsByProductId")
    public String unitsByProductId(Long productId) {

        super.setAttr("units", productOrderService.getUnitList(productId));
        return PREFIX + "/unitsSelect.html";
    }

    @RequestMapping("/troubleType")
    public String troubleType(){
        super.setAttr("troubleTypes",troubleTypeService.list());
        return  PREFIX + "/troubleType.html";
    }


    @RequestMapping("/typeByAfterSale")
    public String typeByAfterSale(){
        List<TypeVO> list = new ArrayList<>();
        TypeVO typeVO1 = new TypeVO(1,"维修");
        TypeVO typeVO2 = new TypeVO(2,"退货");
        list.add(typeVO1);
        list.add(typeVO2);
        super.setAttr("typeList",list);
      return  PREFIX + "/typeSelect.html";
    }


}


